package com.example.music.ui.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.music.R
import com.example.music.data.Music

class RecentMusicAdapter(
    private val onMusicClick: (Music) -> Unit
) : RecyclerView.Adapter<RecentMusicAdapter.RecentMusicViewHolder>() {
    
    private var musicList = listOf<Music>()
    
    fun updateMusicList(newMusicList: List<Music>) {
        musicList = newMusicList
        notifyDataSetChanged()
    }
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecentMusicViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_recent_music, parent, false)
        return RecentMusicViewHolder(view)
    }
    
    override fun onBindViewHolder(holder: RecentMusicViewHolder, position: Int) {
        val music = musicList[position]
        holder.bind(music)
    }
    
    override fun getItemCount(): Int = musicList.size
    
    inner class RecentMusicViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val recentSongTitle: TextView = itemView.findViewById(R.id.recentSongTitle)
        private val recentSongArtist: TextView = itemView.findViewById(R.id.recentSongArtist)
        
        fun bind(music: Music) {
            recentSongTitle.text = music.title
            recentSongArtist.text = music.artist ?: "未知艺术家"
            
            itemView.setOnClickListener {
                onMusicClick(music)
            }
        }
    }
}
